import request from '../utils/request';
import base from './base.js';
import qs from 'qs';
import utils from '../utils';

/*------ Api InSys Begin ------*/

/*------ Api InSys End ------*/

/*------ Api InPro Begin ------*/

const getUserInfo = (query) => {
    return base.getReqeust(query, 'blade-system/user/info');
};

const getUserRoutes = (query) => {
    return base.getReqeust(query, 'blade-system/menu/routes');
};

const getUserButtons = (query) => {
    return base.getReqeust(query, 'blade-system/menu/buttons');
};

const updateUserPassword = (query) => {
    return base.postReqeust(query, 'blade-system/user/update-password');
};

const getCapsuleInfo = (query) => {
    return base.getReqeust(query, 'blade-desk/dashboard/capsule-info');
};

const getUserList = (query) => {
    return base.getReqeust(query, 'blade-system/user/list');
};

const updateUserPasswordInit = (query) => {
    return base.postReqeust(query, 'blade-system/user/reset-password');
};

const deleteUser = (query) => {
    return base.postReqeust(query, 'blade-system/user/remove');
};

const getUserDetail = (query) => {
    return base.getReqeust(query, 'blade-system/user/detail');
};

const getRoleList = (query) => {
    return base.getReqeust(query, 'blade-system/role/list');
};

const saveRole = (query) => {
    return base.postReqeust(query, 'blade-system/role/submit', true);
};

const deleteRole = (query) => {
    return base.postReqeust(query, 'blade-system/role/remove');
};

const getRoleDetail = (query) => {
    return base.getReqeust(query, 'blade-system/role/detail');
};

const getRoleTree = (query) => {
    return base.getReqeust(query, 'blade-system/role/tree-by-id');
};

const getRoleGrantTree = (query) => {
    return base.getReqeust(query, 'blade-system/menu/grant-tree');
};

const getRoleGrantList = (query) => {
    return base.getReqeust(query, 'blade-system/menu/role-tree-keys');
};

const saveRoleGrant = (query) => {
    return base.postReqeust(query, 'blade-system/role/grant', true);
};

const getMenuList = (query) => {
    return base.getReqeust(query, 'blade-system/menu/list');
};

const saveMenu = (query) => {
    return base.postReqeust(query, 'blade-system/menu/submit', true);
};

const deleteMenu = (query) => {
    return base.postReqeust(query, 'blade-system/menu/remove');
};

const getMenuDetail = (query) => {
    return base.getReqeust(query, 'blade-system/menu/detail');
};

const getMenuTree = (query) => {
    return base.getReqeust(query, 'blade-system/menu/tree');
};

const getDepartmentList = (query) => {
    return base.getReqeust(query, 'blade-system/dept/list');
};

const saveDepartment = (query) => {
    return base.postReqeust(query, 'blade-system/dept/submit', true);
};

const deleteDepartment = (query) => {
    return base.postReqeust(query, 'blade-system/dept/remove');
};

const getDepartmentDetail = (query) => {
    return base.getReqeust(query, 'blade-system/dept/detail');
};

const getDepartmentTree = (query) => {
    return base.getReqeust(query, 'blade-system/dept/tree');
};

const getPositionList = (query) => {
    return base.getReqeust(query, 'blade-system/post/list');
};

const savePosition = (query) => {
    return base.postReqeust(query, 'blade-system/post/submit', true);
};

const deletePosition = (query) => {
    return base.postReqeust(query, 'blade-system/post/remove');
};

const getTenantList = (query) => {
    return base.getReqeust(query, 'blade-system/tenant/list');
};

const saveTenant = (query) => {
    return base.postReqeust(query, 'blade-system/tenant/submit', true);
};

const getTenantDetail = (query) => {
    return base.getReqeust(query, 'blade-system/tenant/detail');
};

const deleteTenant = (query) => {
    return base.postReqeust(query, 'blade-system/tenant/remove');
};

const getDevelopList = (query) => {
    return base.getReqeust(query, 'blade-develop/code/list');
};

const saveDevelop = (query) => {
    return base.postReqeust(query, 'blade-develop/code/submit', true);
};

const deleteDevelop = (query) => {
    return base.postReqeust(query, 'blade-develop/code/remove');
};

const runDevelop = (query) => {
    return base.postReqeust(query, 'blade-develop/code/gen-code', false);
};

/*------ Api InPro End ------*/

/*------ Api InSys Begin ------*/

const getCategoryList = (query) => {
    return base.getReqeust(query, 'blade-desk/category/list');
};

const saveCategory = (query) => {
    return base.postReqeust(query, 'blade-desk/category/submit', true);
};

const deleteCategory = (query) => {
    return base.postReqeust(query, 'blade-desk/category/remove');
};

const getCategoryTree = (query) => {
    return base.getReqeust(query, 'blade-desk/category/tree');
};

const getPublishList = (query) => {
    return base.getReqeust(query, 'blade-desk/publish/list');
};

const savePublish = (query) => {
    return base.postReqeust(query, 'blade-desk/publish/submit', true);
};

const deletePublish = (query) => {
    return base.postReqeust(query, 'blade-desk/publish/remove');
};

const getTerminalList = (query) => {
    return base.getReqeust(query, 'blade-desk/terminal/list');
};

const saveTerminal = (query) => {
    return base.postReqeust(query, 'blade-desk/terminal/submit', true);
};

const deleteTerminal = (query) => {
    return base.postReqeust(query, 'blade-desk/terminal/remove');
};

const getComprelList = (query) => {
    return base.getReqeust(query, 'blade-desk/comprel/list');
};

const saveComprel = (query) => {
    return base.postReqeust(query, 'blade-desk/comprel/submit', false);
};

const deleteComprel = (query) => {
    return base.postReqeust(query, 'blade-desk/comprel/remove');
};

const getLogUsualList = (query) => {
    return base.getReqeust(query, 'blade-log/usual/list');
};

const getLogApiList = (query) => {
    return base.getReqeust(query, 'blade-log/api/list');
};

const getLogErrorList = (query) => {
    return base.getReqeust(query, 'blade-log/error/list');
};

const getFeedbackList = (query) => {
    return base.getReqeust(base.queryWithToken(query), 'blade-desk/feedback/page');
};

const deleteFeedback = (query) => {
    return base.postReqeust(base.queryWithToken(query), 'ad_feedback_delete');
};

const getNoteList = (query) => {
    return base.getReqeust(base.queryWithToken(query), 'blade-desk/pushes/page');
};

const getCommentList = (query) => {
    return base.getReqeust(base.queryWithToken(query), 'blade-desk/comment/page');
};

const getMembershipList = (query) => {
    return base.getReqeust(base.queryWithToken(query), 'blade-desk/membership/page');
};

const saveMembership = (query) => {
    return base.postReqeust(query, 'blade-desk/membership/submit', true);
};

/*------ Api InSys End ------*/

const mergePermissions = (p1, p2) => {
    var permissions = p1;
    if (p2 === undefined) {
        return permissions;
    }
    if (permissions === undefined) {
        permissions = p2;
        return permissions;
    }
    for (var i = 0; i < permissions.length; i++) {
        permissions[i].checked = permissions[i].checked === 1 || p2[i].checked === 1 ? 1 : 0;
        // for (var j = 0; j < permissions[i].items.length; j++) {
        //     permissions[i].items[j].checked = permissions[i].items[j].checked === 1 || p2[i].items[j].checked === 1 ? 1 : 0;
        // }
    }
    return permissions;
};

const getUserMenuAndButtons = (resLogin, resolve, reject) => {
    getUserRoutes({}).then(resRoutes => {
        window.localStorage.setItem('routes', JSON.stringify(resRoutes));

        getUserButtons({}).then(resButtons => {
            window.localStorage.setItem('buttons', JSON.stringify(resButtons));

            if (resLogin) {
                resolve(resLogin);
            }
            // if (query.account === 'admin') {
            //     resolve(resLogin.data);
            //     return;
            // }
        }).catch(err => {
            reject(err);
        });
    }).catch(err => {
        reject(err);
    });
};

const api = {
    getUserMenuAndButtons: (resolve, reject) => {
        getUserMenuAndButtons(false, resolve, reject);
    },
    fetchData: query => {
        return base.getReqeust(query, 'table.json');
    },
    login: query => {
        return new Promise((resolve, reject) => {
            var param = Object.assign({}, query, {password: utils.encrypt(query.password)});
            base.postReqeust(param, 'blade-auth/token').then(resLogin => {
                window.localStorage.setItem('token', JSON.stringify(resLogin));

                getUserInfo({}).then(resUsers => {
                    window.localStorage.setItem('profile', JSON.stringify(resUsers));

                    getUserMenuAndButtons(resLogin, resolve, reject);
                }).catch(err => {
                    reject(err);
                });
            }).catch(err => {
                reject(err);
            })
        });
    },
    logout: () => {
        return new Promise((resolve, reject) => {
            resolve();
        });
        // return base.postReqeustSys(base.queryWithToken({}), 'logout');
    },
    updateUserPassword,
    getCapsuleInfo,
    getUserList,
    updateUserPasswordInit,
    deleteUser,
    getUserDetail,
    getRoleList,
    saveRole,
    deleteRole,
    getRoleDetail,
    getRoleTree,
    getRoleGrantTree,
    getRoleGrantList,
    saveRoleGrant,
    getMenuList,
    saveMenu,
    deleteMenu,
    getMenuDetail,
    getMenuTree,
    getDepartmentList,
    saveDepartment,
    deleteDepartment,
    getDepartmentDetail,
    getDepartmentTree,
    getPositionList,
    savePosition,
    deletePosition,
    getTenantList,
    saveTenant,
    getTenantDetail,
    deleteTenant,
    getDevelopList,
    saveDevelop,
    deleteDevelop,
    runDevelop,
    getCategoryList,
    saveCategory,
    deleteCategory,
    getCategoryTree,
    getPublishList,
    savePublish,
    deletePublish,
    getTerminalList,
    saveTerminal,
    deleteTerminal,
    getComprelList,
    saveComprel,
    deleteComprel,
    getLogUsualList,
    getLogApiList,
    getLogErrorList,
    getFeedbackList,
    deleteFeedback,
    getNoteList,
    getCommentList,
    getMembershipList,
    saveMembership,
    permission: {
        getAll: () => {
            let str = window.localStorage.getItem('permissions');
            return JSON.parse(str);
        },
        haveGot: (right) => {
            let str = window.localStorage.getItem('permissions');
            let permissions = JSON.parse(str);
            let arrRight = right.split('|');
            for (var j = 0; j < arrRight.length; j++) {
                for (var i = 0; i < permissions.length; i++) {
                    for (var k = 0; k < permissions[i].items.length; k++) {
                        if (permissions[i].items[k].model_name === arrRight[j] || permissions[i].items[k].name === arrRight[j]) {
                            if (permissions[i].items[k].checked === 1) {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        },
        isContained: (right) => {
            let str = window.localStorage.getItem('permissions');
            let pers = JSON.parse(str);
            let arrRight = right.split('|');
            for (var j = 0; j < arrRight.length; j++) {
                var rite = arrRight[j];
                for (var i = 0; i < pers.length; i++) {
                    var element = pers[i];
                    if (element.name === rite || element.model_name === rite) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
};

export default api;
